package com.wisdudu.blank.ui.user;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.wisdudu.blank.R;
import com.wisdudu.blank.databinding.FragmentUserinfoNickEditBinding;
import com.wisdudu.frame.base.BaseFragment;

/**
 * Created by ypp on 2016/8/29.
 */
public class UserNickEditFragment extends BaseFragment {
    public static final String EXTRA_NICKNAME = "nickName";
    private UserNickEditVM userNickEditVM;

    @Override
    protected View initBinding(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        FragmentUserinfoNickEditBinding mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_userinfo_nick_edit, container, false);
        userNickEditVM = new UserNickEditVM(this, getArguments().getString(EXTRA_NICKNAME));
        mBinding.setViewModel(userNickEditVM);
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initToolbar(getToolbar(), "修改昵称");
    }

    @Override
    protected void initMenuToolbar(Toolbar toolbar) {
        super.initMenuToolbar(toolbar);
        toolbar.inflateMenu(R.menu.menu_fragment_save);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.menu_save:
                        confirm();
                        break;
                }
                return true;
            }
        });
    }

    private void confirm() {
        hideKeyboard();
        userNickEditVM.confirm();
    }
}
